package time;

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

public class TimerDemo {
	//这种一直运行
	private static Timer timer=new Timer();
	//变成守护线程,执行完就退出
	private static Timer timer1=new Timer(true);
	private static class MyTask extends TimerTask{
		/**
		 * timer 类是清除这个类里面的所有任务
		 * timerTask类是清除这个类的task
		 */
		@Override
		public void run() {
			System.out.println("当前时间"+System.currentTimeMillis());
		}
		
	}
	public static void main(String[] args) {
		//MyTask myTask=new MyTask();
		MyTask myTask1=new MyTask();
		/*timer.schedule(myTask, new Date(System.currentTimeMillis()));
		timer.schedule(task, firstTime, period);//在first时候执行,每隔period时间执行一次
		timer.schedule(task, delay);//延迟执行
		timer.schedule(task, delay, period);//延迟执行,每隔period时间执行一次
		timer.scheduleAtFixedRate(task, firstTime, period);//在不延时的情况下,和schedule区别在于他是按结束时间开始计时的
		timer.scheduleAtFixedRate(task, delay, period);
*/
		System.out.println(System.currentTimeMillis());
		timer1.schedule(myTask1, new Date(System.currentTimeMillis()+1000));
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			
			e.printStackTrace();
		}
	}
}
